module.exports = {
  css: {
    loaderOptions: {
      stylus: {
        'resolve url': true,
        'import': [
          './src/theme'
        ]
      }
    }
  },
  pluginOptions: {
    'cube-ui': {
      postCompile: true,
      theme: true
    }
  },
  configureWebpack: {
    devServer: {
      host: '0.0.0.0',
      hot: true,
      disableHostCheck: true,
      before (app) {
        const API_KEY = 'oktologin'

        app.get('/api/authapi', function (req, res) {
          const { token } = req.headers
          if (!token) {
            return res.json({ code: -1 })
          }
          const [key, expires] = token.split('-')
          const now = new Date().getTime()
          if (key === API_KEY && expires > now) {
            return res.json({
              code: 0,
              data: '通过校验'
            })
          } else {
            return res.json({
              code: -1,
              message: '登录授权过期'
            })
          }
        })

        app.get('/api/check', function (req, res) {
          const username = req.query.username
          if (username === 'xuan') {
            res.json({ code: 1 })
          } else {
            res.json({ code: 0 })
          }
        })

        app.get('/api/logout', function (req, res) {
          res.json({
            code: -1
          })
        })

        app.get('/api/goods', function (req, res) {
          res.json({
            code: 0,
            slider: [
              {
                id: 21,
                img: 'https://1img.hitv.com/preview/sp_images/2019/1/6/zongyi/326912/4937881/20190106000315739.jpg'
              },
              {
                id: 22,
                img: 'https://1img.hitv.com/preview/sp_images/2018/12/1/zongyi/325963/4780075/20181201233117711.jpg'
              },
              {
                id: 23,
                img: 'https://2img.hitv.com/preview/sp_images/2017/zongyi/309556/3789850/20170119172737265.jpg'
              },
              {
                id: 24,
                img: 'https://3img.hitv.com/preview/sp_images/2018/12/23/zongyi/325963/4849726/20181223095319256.jpg'
              }
            ],
            data: {
              fe: [
                {
                  id: 1,
                  title: 'Vue2.x实战',
                  price: '100',
                  img: 'https://0img.hitv.com/preview/sp_images/2019/1/4/zongyi/325963/4925364/20190104092235900.jpg',
                  count: 100
                },
                {
                  id: 2,

                  title: 'React16.x实战',
                  price: '120',
                  img: 'https://3img.hitv.com/preview/sp_images/2018/11/17/zongyi/325963/4738688/20181117230324808.jpg',
                  count: 100
                },
                {
                  id: 3,

                  title: 'nodejs实战',
                  price: '80',
                  img: 'https://3img.hitv.com/preview/sp_images/2018/12/9/zongyi/325963/4800877/20181209023020082.jpg',
                  count: 100
                },
                {
                  id: 4,

                  title: '前端工程化',
                  price: '110',
                  img: 'https://0img.hitv.com/preview/sp_images/2018/zongyi/322705/4399051/20180521175210213.jpg',
                  count: 100
                },
                {
                  id: 5,

                  title: '面试',
                  price: '200',
                  img: 'https://2img.hitv.com/preview/sp_images/2017/zongyi/317663/4157493/20171102124331092.jpg',
                  count: 100
                },
                {
                  id: 6,

                  title: '前端安全',
                  price: '30',
                  img: 'https://1img.hitv.com/preview/sp_images/2018/zongyi/325963/4694744/20181104131436125.jpg',
                  count: 100
                }
              ],
              python: [

                {
                  id: 7,

                  title: 'Python基础语法',
                  price: '120',
                  img: 'https://3img.hitv.com/preview/sp_images/2018/11/29/zongyi/327200/4772831/20181129123732435.jpg',
                  count: 101
                },
                {
                  id: 8,
                  title: 'Flask实战',
                  price: '80',
                  img: 'https://2img.hitv.com/preview/sp_images/2018/zongyi/326912/4692528/20181103202306590.jpg',
                  count: 100
                },
                {
                  id: 9,
                  title: 'Django实战',
                  price: '110',
                  img: 'https://3img.hitv.com/preview/sp_images/2018/11/10/zongyi/326912/4715632/20181110225510656.jpg',
                  count: 100
                },
                {
                  id: 10,
                  title: 'Python语法进阶',
                  price: '200',
                  img: 'https://2img.hitv.com/preview/sp_images/2018/11/17/zongyi/326912/4738436/20181117221055100.jpg',
                  count: 100
                }
              ],
              java: [

                {
                  id: 11,
                  title: 'java入门实战',
                  price: '80',
                  img: 'https://3img.hitv.com/preview/sp_images/2018/11/24/zongyi/326912/4759449/20181124211754143.jpg',
                  count: 100
                },
                {
                  id: 12,
                  title: 'spring boot实战',
                  price: '110',
                  img: 'https://0img.hitv.com/preview/internettv/sp_images/ott/2018/zongyi/325963/20181025145441810-new.jpg',
                  count: 100
                },
                {
                  id: 13,
                  title: 'Java高并发',
                  price: '30',
                  img: 'https://0img.hitv.com/preview/sp_images/2018/12/1/zongyi/326912/4780065/20181201231731768.jpg',
                  count: 100
                }
              ],
              bigdata: [
                {
                  id: 14,
                  title: '大数据实战',
                  price: '200',
                  img: 'https://1img.hitv.com/preview/sp_images/2018/12/21/zongyi/325963/4841019/20181221034416166.jpg',
                  count: 100
                },
                {
                  id: 15,
                  title: 'Hadoop实战',
                  price: '120',
                  img: 'https://3img.hitv.com/preview/sp_images/2018/12/6/zongyi/325963/4794025/20181206185511558.jpg',
                  count: 100
                },
                {
                  id: 16,
                  title: 'Kafka平台',
                  price: '80',
                  img: 'https://2img.hitv.com/preview/sp_images/2018/zongyi/325963/4665373/20181025145841863.jpg',
                  count: 100
                }

              ],
              ai: [
                {
                  id: 17,
                  title: '算法实战',
                  price: '100',
                  img: 'https://0img.hitv.com/preview/sp_images/2018/11/7/zongyi/291976/3272630/20181107225957173.jpg',
                  count: 100
                },
                {
                  id: 18,
                  title: '个性化推荐',
                  price: '120',
                  img: 'https://3img.hitv.com/preview/cms_icon/2019/1/5/01/20190105215939065.jpg',
                  count: 100
                },
                {
                  id: 19,
                  title: '机器学习',
                  price: '80',
                  img: 'https://1img.hitv.com/preview/cms_icon/2018/12/18/12/20181218120803800.jpg',
                  count: 100
                },
                {
                  id: 20,
                  title: 'AI实战',
                  price: '110',
                  img: 'https://3img.hitv.com/preview/cms_icon/2018/12/18/12/20181218120702944.jpg',
                  count: 100
                }
              ]
            },
            keys: ['fe', 'python', 'java', 'bigdata', 'ai']

          })
        })

        app.get('/api/login', function (req, res) {
          const { username, passwd } = req.query
          if (username === 'zixuan' && passwd === '123') {
            res.json({
              code: 0,
              token: API_KEY + '-' + (new Date().getTime() + 1000 * 10)
            })
          } else {
            res.json({
              code: 1
            })
          }
        })
      }
    }
  }
}
